var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
//引入线下活动路由
var activityRouter = require("./routes/activity");
var findRouter = require("./routes/find");
var index1Router = require("./routes/index1");
var index2Router = require("./routes/index2");
var mapRouter = require("./routes/map");
var msgRouter = require("./routes/msg");
var slowreadRouter = require("./routes/slowread");
var storeRouter = require("./routes/store");
var teachRouter = require("./routes/teach");
var loginRouter = require("./routes/login");
const cors = require("cors");
var app = express();

// getting-started.js
var mongoose = require("mongoose");
mongoose.connect("mongodb://121.199.64.192:27017/fandeng");

var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function () {
  console.log("连接成功");
});

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use(cors());

app.use("/", indexRouter);
app.use("/users", usersRouter);
//调用线下活动路由
app.use("/api/activity", activityRouter);
app.use("/api/find", findRouter);
app.use("/api/index1", index1Router);
app.use("/api/index2", index2Router);
app.use("/api/map", mapRouter);
app.use("/api/msg", msgRouter);
app.use("/api/slowread", slowreadRouter);
app.use("/api/store", storeRouter);
app.use("/api/teach", teachRouter);
app.use("/api/login", loginRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;
